package com.andatsoft.laisim.visualizer.renderer;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.andatsoft.laisim.visualizer.AudioData;
import com.andatsoft.laisim.visualizer.FFTData;

public class PointLineRenderer extends Renderer {

	private Paint mPaint;
	private Paint mFadeInPaint;
	private boolean mCycleColor;
	private float[] rPoint = new float[128];

	public PointLineRenderer(Paint paint, boolean cycle) {
		this.mPaint = paint;
		mFadeInPaint = new Paint();
		mFadeInPaint.setColor(Color.argb(60, 255, 255, 255));
		this.mCycleColor = cycle;
	}

	@Override
	public void onRender(Canvas canvas, AudioData data, Rect rect) {
		if (mCycleColor) {
			cycleColor();
		}

		// Log.e("leng", data.bytes.length+"");
		// Calculate points for line
		int j = 0;
		for (int i = 0; i < data.bytes.length - 1; i++) {
			mPoints[i * 4] = rect.width() * i / (data.bytes.length - 1);
			mPoints[i * 4 + 1] = rect.height() / 2
					+ ((byte) (data.bytes[i] + 128)) * (rect.height() / 3)
					/ 128;
			mPoints[i * 4 + 2] = rect.width() * (i + 1)
					/ (data.bytes.length - 1);
			mPoints[i * 4 + 3] = rect.height() / 2
					+ ((byte) (data.bytes[i + 1] + 128)) * (rect.height() / 3)
					/ 128;
			// if(i%8==0)
			// {
			// rPoint[j*4] = mPoints[i*4];
			// rPoint[j*4+1] = mPoints[i*4+1];
			// rPoint[j*4+2] = mPoints[i*4+2];
			// rPoint[j*4+3] = mPoints[i*4+3];
			// j++;
			// }

		}
		// get the real point
		int flag = 0;
		while (flag < rPoint.length - 1) {
			int i = new Random().nextInt(data.bytes.length - 1);
			rPoint[j * 4] = mPoints[i * 4];
			rPoint[j * 4 + 1] = mPoints[i * 4 + 1];
			rPoint[j * 4 + 2] = mPoints[i * 4 + 2];
			rPoint[j * 4 + 3] = mPoints[i * 4 + 3];
			j++;
			flag += 8;
		}
		// toRPoint();
		// Calc amplitude for this waveform
		float accumulator = 0;
		for (int i = 0; i < data.bytes.length - 1; i++) {
			accumulator += Math.abs(data.bytes[i]);
		}
		// Log.e("accumulator", accumulator + "");
		float amp = accumulator / (128 * data.bytes.length);
		// Log.e("amp", amp + "," + amplitude);
		if (amp > 0.9f) {

			// Amplitude is bigger than normal, make a prominent line
			// amplitude = amp;
			// mMatrix.postTranslate(dx, dy)
			canvas.drawCircle(new Random().nextInt(rect.width()),
					new Random().nextInt(rect.height()),
					new Random().nextInt(800) * 0.1f, mPaint);

		}
		// bass
		float max = 0;
		float min = 1000;
		for (int i = 1; i < mPoints.length - 1; i += 2) {
			if (max < (mPoints[i]))
				max = mPoints[i];
			if (min > mPoints[i])
				min = mPoints[i];

		}
		// Log.e("Maxax", (max-min)+"");
		if (max - min > rect.height() * 4 / 5) {
			canvas.drawPaint(mFadeInPaint);
		}

		// canvas.drawPoints(mPoints, mPaint);
		canvas.drawPoints(rPoint, mPaint);

	}

	@Override
	public void onRender(Canvas canvas, FFTData data, Rect rect) {

	}

	private float colorCounter = 0;

	private void cycleColor() {
		int r = (int) Math.floor(128 * (Math.sin(colorCounter) + 2));
		int g = (int) Math.floor(128 * (Math.sin(colorCounter + 1) + 1));
		int b = (int) Math.floor(128 * (Math.sin(colorCounter + 1) + 3));
		mPaint.setColor(Color.argb(220, r, g, b));
		colorCounter += 0.03;
	}

}
